package com.imageim.yimirror.ui.adapter.tryCloth;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseActivity;
import com.imageim.yimirror.base.BaseApplication;
import com.imageim.yimirror.base.Constants;
import com.imageim.yimirror.bean.dailyPhoto.TryClothResultPhotoBean;
import com.imageim.yimirror.common.network.NetUtil;
import com.imageim.yimirror.common.network.constants.NetConstants;
import com.imageim.yimirror.common.network.listener.NetCallback;
import com.imageim.yimirror.common.network.tool.ParamCreator;
import com.imageim.yimirror.common.ui.GlideUtils;
import com.imageim.yimirror.common.ui.tip.TipUtil;
import com.imageim.yimirror.common.util.ClickUtil;
import com.makeramen.roundedimageview.RoundedImageView;

import org.apache.http.message.BasicNameValuePair;
import org.byteam.superadapter.IMulItemViewType;
import org.byteam.superadapter.SuperAdapter;
import org.byteam.superadapter.SuperViewHolder;

import java.util.List;

/**
 * 试穿留影背景替换后的照片适配器
 * Created by Bruce.Zhou on 2018/11/22 11:36.
 * Email: 907160968@qq.com
 */
public class TryClothResultPhotoAdapter extends SuperAdapter<TryClothResultPhotoBean> {
    private int red = Color.parseColor(BaseApplication.getContext().getString(R.string.border_color));

    public TryClothResultPhotoAdapter(Context context, List<TryClothResultPhotoBean> items, int layoutResId) {
        super(context, items, layoutResId);
    }

    public TryClothResultPhotoAdapter(Context context, List<TryClothResultPhotoBean> items, IMulItemViewType<TryClothResultPhotoBean> mulItemViewType) {
        super(context, items, mulItemViewType);
    }

    @Override
    public void onBind(SuperViewHolder holder, int viewType, int layoutPosition, TryClothResultPhotoBean item) {
        holder.setOnClickListener(R.id.iv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ClickUtil.isValidClick(550)) {
                    List<BasicNameValuePair> params = new ParamCreator()
                            .add(Constants.PHOTO_NO, item.getPhotoNo())
                            .build();
                    NetUtil.postObject(getContext(), NetConstants.URL.DELETE_IMAGE_PHOTO, params, new NetCallback<String>() {
                        @Override
                        public void onStart() {
                            ((BaseActivity)getContext()).showLoading();
                        }

                        @Override
                        public void onSuccess(String result) {
                            if (layoutPosition < getData().size()) {
                                getData().remove(layoutPosition);
                                notifyItemRemoved(layoutPosition);
                            }
                            ((BaseActivity)getContext()).hideLoading();
                        }

                        @Override
                        public void onFail(int errorCode, String errorMsg) {
                            TipUtil.show("删除失败");
                            ((BaseActivity)getContext()).hideLoading();
                        }
                    });
                }
            }
        });
        RoundedImageView iv = holder.findViewById(R.id.iv_photo);
        if (!TextUtils.isEmpty(item.getBodyComposeImage())) {
            GlideUtils.display(iv, item.getBodyComposeImage());
        } else if (!TextUtils.isEmpty(item.getPhotoUrl())) {
            GlideUtils.display(iv, item.getPhotoUrl());
        } else {
            holder.setImageResource(R.id.iv_photo, R.drawable.filter1);
        }

        if (item.isSelected()) {
            iv.setBorderColor(red);
        } else {
            iv.setBorderColor(Color.TRANSPARENT);
        }

    }

    public void select(int position) {
        for (int i = 0; i < getData().size(); i++) {
            if (i != position) {
                getData().get(i).setSelected(false);
            } else {
                getData().get(i).setSelected(true);
            }
        }
        notifyDataSetChanged();
    }


}
